<?php
/**
 * 统一初始化
 */

/** ---------------- 根目录定义，自动加载 ---------------- **/
error_log(0);
date_default_timezone_set('Asia/Shanghai');
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Headers:X-Requested-With");
header("Access-Control-Allow-Methods:GET,POST,OPTIONS");

defined('API_ROOT') || define('API_ROOT', dirname(__FILE__) . '/..');

require_once API_ROOT . '/PhalApi/PhalApi.php';
$loader = new PhalApi_Loader(API_ROOT, 'Library');

/** ---------------- 注册&初始化 基本服务组件 ---------------- **/

//自动加载
DI()->loader = $loader;

//配置
DI()->config = new PhalApi_Config_File(API_ROOT . '/Config');

//调试模式，$_GET['__debug__']可自行改名
DI()->debug = !empty($_GET['__menma__debug__']) ? true : DI()->config->get('sys.debug');

//日记纪录
DI()->logger = new PhalApi_Logger_File(API_ROOT . '/Runtime', PhalApi_Logger::LOG_LEVEL_DEBUG | PhalApi_Logger::LOG_LEVEL_INFO | PhalApi_Logger::LOG_LEVEL_ERROR);


//数据操作 - 基于NotORM，$_GET['__sql__']可自行改名
DI()->notorm = new PhalApi_DB_NotORM(DI()->config->get('dbs'), !empty($_GET['__menma__debug__']));


//DI()->mongo=new MongoDB_Lite(DI()->config->get('dbs.mongoDB'));

//翻译语言包设定
SL('zh_cn');

/** ---------------- 定制注册 可选服务组件 ---------------- **/
// DI()->base= new common_base();


if (file_get_contents("php://input")) {
    $data = @array_merge($_GET, @json_decode(file_get_contents("php://input"), true));
    DI()->request = new PhalApi_Request($data);
}

//缓存 - redis
DI()->cache = new Redis_Lite(DI()->config->get('app.redis.servers'));


//钉钉服务
DI()->DingTalk = new DingTalk_Lite();

//又拍云上传
DI()->UCloud = new UCloud_Lite();

//邮件服务
DI()->mailer = new PHPMailer_Lite(true);

DI()->wechat = new Wechat_Lite(DI()->config->get('app.Wechat'));

//接口限制
DI()->filter = "Common_Limit";


DI()->helper = "Common_Helper";

//DI()->SMS = new SMS_Lite('app.SMSService');

//支持JsonP的返回 和数据错误记录
DI()->response = new Raven_Lite(DI()->config->get('app.SentryDSN'),!empty($_GET['callback'])?$_GET['callback']:null);



//支持JsonP的返回
//if (!empty($_GET['callback'])) {
//    DI()->response = new PhalApi_Response_JsonP($_GET['callback']);
//}

//路由伪静态 需配置服务器
//DI()->fastRoute = new FastRoute_Lite();
//DI()->fastRoute->dispatche();
